
$(function(){


    //Sidebar Accordion Menu:
		
    $("#main-nav li ul").hide(); // Hide all sub menus
    $("#main-nav li a.current").parent().find("ul").slideToggle("slow"); // Slide down the current menu item's sub menu
		
    $("#main-nav li a.nav-top-item").click( // When a top menu item is clicked...
        function () {
            $(this).parent().siblings().find("ul").slideUp("normal"); // Slide up all sub menus except the one clicked
            $(this).next().slideToggle("normal"); // Slide down the clicked sub menu
            return false;
        }
        );
		
    $("#main-nav li a.no-submenu").click( // When a menu item with no sub menu is clicked...
        function () {
            window.location.href=(this.href); // Just open the link instead of a sub menu
            return false;
        }
        ); 

    // Sidebar Accordion Menu Hover Effect:
		
    $("#main-nav li .nav-top-item").hover(
        function () {
            $(this).stop().animate({
                paddingRight: "25px"
            }, 200);
        }, 
        function () {
            $(this).stop().animate({
                paddingRight: "15px"
            });
        }
        );

    //Minimize Content Box
		
    $(".content-box-header h3").css({
        "cursor":"s-resize"
    }); // Give the h3 in Content Box Header a different cursor
    $(".closed-box .content-box-content").hide(); // Hide the content of the header if it has the class "closed"
    $(".closed-box .content-box-tabs").hide(); // Hide the tabs in the header if it has the class "closed"
		
    $(".content-box-header h3").click( // When the h3 is clicked...
        function () {
            $(this).parent().next().toggle(); // Toggle the Content Box
            $(this).parent().parent().toggleClass("closed-box"); // Toggle the class "closed-box" on the content box
            $(this).parent().find(".content-box-tabs").toggle(); // Toggle the tabs
        }
        );

    // Content box tabs:
		
    $('.content-box .content-box-content div.tab-content').hide(); // Hide the content divs
    $('ul.content-box-tabs li a.default-tab').addClass('current'); // Add the class "current" to the default tab
    $('.content-box-content div.default-tab').show(); // Show the div with class "default-tab"
		
    $('.content-box ul.content-box-tabs li a').click( // When a tab is clicked...
        function() { 
            $(this).parent().siblings().find("a").removeClass('current'); // Remove "current" class from all tabs
            $(this).addClass('current'); // Add class "current" to clicked tab
            var currentTab = $(this).attr('href'); // Set variable "currentTab" to the value of href of clicked tab
            $(currentTab).siblings().hide(); // Hide all content divs
            $(currentTab).show(); // Show the content div with the id equal to the id of clicked tab
            return false; 
        }
        );

    //Close button:
		
    $(".close").click(
        function () {
            $(this).parent().fadeTo(400, 0, function () { // Links with the class "close" will close parent
                $(this).slideUp(400);
            });
            return false;
        }
        );

    // Alternating table rows:
		
    $('tbody tr:even').addClass("alt-row"); // Add class "alt-row" to even table rows

    // Check all checkboxes when the one in a table head is checked:
		
    $('.check-all').click(
        function(){
            $(this).parent().parent().parent().parent().find("input[type='checkbox']").attr('checked', $(this).is(':checked'));   
        }
        );

    // Initialise Facebox Modal window:
		
    $('a[rel*=modal]').facebox(); // Applies modal window to any link with attribute rel="modal"
});
  
  
      function edit_item(){
        $('.edit_item_popup').click(function(){
            var item_id = $(this).attr('alt');
            $('#data').dialog( "destroy" );
            $('#data').dialog({

                title: 'Redaguoti preke',
                resizable: true,
                modal: false,
                width: 800,
                position: 'top',
                buttons: {
                    'Uždaryti': function() {
                        $('#data').dialog('close');
                    }
                },
                open: function() {
                    $.post(base_url+'administration/admin_redaguoti_preke/preke/'+item_id, function(data) {
                        $('#data').html(data); 
                                   
                        var icons = {
                            header: "ui-icon-circle-arrow-e",
                            headerSelected: "ui-icon-circle-arrow-s"
                        };
                        $( "#accordion" ).accordion({
                            icons: icons,
                            'autoHeight' : false
                        });
                        $( "#toggle" ).button().toggle(function() {
                            $( "#accordion" ).accordion( "option", "icons", false );
                        }, function() {
                            $( "#accordion" ).accordion( "option", "icons", icons );
                        });
                                    
                        $(".expire_date").datepicker({
                            dateFormat: 'yy-mm-dd',
                            showOn: 'button',
                            buttonImage: base_url + 'images/calendar.gif',
                            buttonImageOnly: true,
                            showButtonPanel: true,
                            changeMonth: true,
                            changeYear: true
                        });
                        $("select[name='importuotojas']").change(function(){
                            $('.vendor_list').html('<i>Įkeliama...</i>');
                            $.post( base_url + 'administration/admin_nauja_preke/get_vendors/'+$(this).attr('value'),function(data){
                                $('.vendor_list').html(data);
                            });
                        });
                        $('.save_item_info').click(function(){
                            $.post(base_url + 'administration/admin_redaguoti_preke/submit/'+item_id , $('#edit_preke').serialize(), function(data){
                                $('.item_edit_info').html('<div class="notification success png_bg" ><a href="#" class="close" ></a><div>Prekės informacija sėkmingai atnaujinta.</div></div>');
                                setTimeout(function(){
                                    $('.item_edit_info').fadeOut("slow")
                                }, 2000);
                            }) 
                        });
                        $('.save_spec').click(function(){
                            $.post(base_url + 'administration/admin_redaguoti_preke/submit_spec/'+item_id , $('#new_item_spec').serialize(), function(data){
                                $('#specification_list > tbody:last').append('<tr><th>'+$('input[name="specifikacija"]').val()+'</th><td>'+$('.spec_desc').val()+'</td><td></td></tr>');
                            //alert(data);
                            });  
                        });
                        $('.delete_spec').click(function(){ 
                            var spec_id = $(this).attr('spec_id');
                            if(confirm('Ištrinti specifikacija?')){
                                $.post(base_url + 'administration/admin_redaguoti_preke/delete_spec/'+spec_id , $('#new_item_spec').serialize(), function(){
                                    $('#row_'+spec_id).remove(); 
                                });   
                            }
                                        
                        });
                        $('.delete_foto').click(function(){
                            $.post( base_url+'administration/admin_redaguoti_preke/delete_foto', {
                                'url' : $(this).attr('img_url')
                            }, function(){
                                $(this).parent().fadeOut("slow");
                            });
                        });
                        $("#upload").uploadify({
                            'uploader'       : base_url+'js/upload/uploadify.swf',
                            'script'         : base_url+'administration/admin_redaguoti_preke/upload_foto/'+item_id,
                            // 'buttonImage'   : base_url+'js/upload/upload.png',
                            'folder'   : '../product_images',
                            'auto'           : true,
                            'multi'          : true,
                            'scriptData'     : {
                                id : item_id
                            },
                            'buttonText'     : 'Pasirinkti',
                            'onComplete'     : function(event, queueID, fileObj, response, data) {
                                $('.images_list').append('<div style="float: left; width: 105px;">'+
                                    '<a href="javascript:void(1);"><img src="'+base_url+'img/delete_a.gif" class="delete_foto" alt="Trinti nuotrauką" title="Trinti nuotrauką" img_url="'+response+'" /></a><br/>'+
                                    '<img class="image" src="'+base_url+'product_images/'+response+'" /></div>');

                                $('.delete_foto').click(function(){
                                    $.post( base_url+'administration/admin_redaguoti_preke/delete_foto', {
                                        'url' : $(this).attr('img_url')
                                    }, function(){
                                        $(this).parent().fadeOut("slow");
                                    });
                                });

                            }
                        });
                    } );
                }
            });
        });
    }	